home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5924 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.5 KB  |  98 lines

  1. Path: news.ichange.com!newsmaster
  2. From: patrick_widener@mail.amsinc.com (Patrick Widener)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Why is a temporary created here?
  5. Date: Wed, 07 Feb 1996 14:12:36 GMT
  6. Organization: American Management Systems, Inc.
  7. Message-ID: <4fac8v$eur@ias2.ichange.com>
  8. References: <47ts5e$21o@ams.amsinc.com> <47vvhh$9ce@gabi.gabi-soft.fr>
  9. Reply-To: patrick_widener@mail.amsinc.com
  10. NNTP-Posting-Host: 162.70.176.43
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. kanze@gabi-soft.fr (J. Kanze) wrote:
  14.  
  15. >Patrick Widener (patrick_widener@mail.amsinc.com) wrote:
  16. >
  17. >|> I have this situation:
  18. >
  19. >|> class B
  20. >|> { 
  21. >|> ...
  22. >|> };
  23. >
  24. >|> class DOne : public B
  25. >|> {
  26. >|> ... 
  27. >|> };
  28. >
  29. >|> class DTwo : public B
  30. >|> {
  31. >|> public: 
  32. >|>     void alfa ();
  33. >
  34. >|> protected:
  35. >
  36. >|>     void bravo( B* &);
  37. >|> };
  38. >
  39. >|> void DTwo::alfa
  40. >|> {
  41. >|>     DOne *pDOne;
  42. >|>     DTwo *pDTwo;
  43. >|>     
  44. >|>     bravo (pDOne);  // These lines are flagged by the compiler as 
  45. >|>     bravo (pDTwo); // warnings because temporaries are used for
  46. >|>             // the formal argument of DTwo::bravo()
  47. >|> }
  48. >
  49. >|> I can't figure out why my compiler (Borland bcc32 4.51) is complaining
  50. >|> about the lines indicated above.  To me, it seems that the pointers in
  51. >|> DTwo::alfa(), being pointers to types derived from B, should be
  52. >|> implicitly converted to B pointers.  Someone enlighten me, please?
  53. >
  54. >And they are being implicitly converted.  That's exactly what the
  55. >compiler is warning you about.  The result of a conversion is a
  56. >temporary (a non-lvalue).
  57. >
  58. >According to the current draft, binding a non-lvalue to a non-const
  59. >reference is illegal, since any changes made through the reference will
  60. >only modify the temporary.  This change was introduced at a later stage
  61. >in the language evolution (about 1990, I think), and most compilers only
  62. >generate a warning, rather than an error, to avoid breaking existing
  63. >code.  But it is still illegal.
  64. >
  65. >From the above, it is not clear what you really want to do.  Perhaps
  66. >declaring the parameter to bravo const would be enough, although in this
  67. >case, since you are dealing with pointers, just pass be value would have
  68. >the same effect.  Alternatively, you could declare pDOne and pDTwo as
  69. >B*.  Or create two bravo functions, one for each type.  Or use an
  70. >explicit temporary of the correct type, and then assign it back to pDOne
  71. >or pDTwo after bravo returns.
  72. >-- 
  73. >James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  74. >GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  75. >Conseils, Θtudes et rΘalisations en logiciel orientΘ objet --
  76. >              -- A la recherche d'une activitΘ dans une region francophone
  77.  
  78. Hmmm.  OK, this sounds right.  I wasn't as clear as I could have been
  79. in the original statement of the problem.  In particular, the bravo()
  80. method *needs* an lvalue, since what it is doing in real life is
  81. pulling a specific instance of a class derived from B out of a
  82. dictionary maintained by DTwo.  That's why I wanted to pass the
  83. pointer by reference, since I'm going to change it (assign to it) in
  84. bravo().  
  85.  
  86. I thought this was going to work because of the polymorphic behavior
  87. of the pointers, but I seem to remember something about parameter
  88. passing being modeled on initialization, not assignment, which I think
  89. would cause problems.  oh well.
  90.  
  91. thanks for responding...
  92.  
  93. --------
  94. patrick widener
  95. patrick_widener@mail.amsinc.com   http://pwidener.amsinc.com:2112
  96. "Ted Striker has more guts in his big toe than most of us have in
  97. our entire large intestine - INCLUDING the colon!!"
  98.